package top.yuki.goods;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import top.yuki.LevelWorld;
import top.yuki.data.DataManager;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;

public class Prize {
    private static LevelWorld lw = LevelWorld.instance;
    public static DataManager dataManager = lw.dataManage;


    public static void dropPrize(Player player, String prizeName){
        File prizeFolder = new File(lw.getDataFolder()+File.separator+"prize");
        File file = new File(prizeFolder+File.separator+prizeName+ ".yml");
        FileConfiguration c = YamlConfiguration.loadConfiguration(file);
        ItemStack[] content = ((List<ItemStack>) c.get("items")).toArray(new ItemStack[0]);
        for (ItemStack itemStack : content) {
            if (itemStack!=null) {
                itemStack.setAmount(gaussianDistribution(itemStack.getAmount()));
                player.getLocation().getWorld().dropItem(player.getLocation(), itemStack);
            }

        }

    }

    public void setPrize(Player player, String prizeName){
        File file;
        file = init(prizeName);
        ItemStack[] contents = player.getInventory().getContents();

        FileConfiguration c = YamlConfiguration.loadConfiguration(file);
        c.set("items", contents);
        try {
            c.save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public File init(String prizeName){
        File prizeFolder = new File(lw.getDataFolder()+File.separator+"prize");
        File file= new File(prizeFolder+File.separator+prizeName+ ".yml");
        if (!prizeFolder.exists()) {
            prizeFolder.mkdirs();
        }
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return file;

    }

    /*
    输入一个均值，根据正态分布u-3sigma>0原则返回一个随机数
     */
    public static int gaussianDistribution(int u){
        //参数 u 均值
        //参数 v 方差
        // u-3sigma>0


        double u1 = u;
        double v = u1/3;
        double result;

        Random random = new java.util.Random();
        result = v * random.nextGaussian() + u;
        int result1 = Math.toIntExact(Math.round(result));

        return result1;
    }

}
